package com.ruoyi.module.szLiveRoom.service.impl;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.module.szLiveRoom.util.ZBUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.module.szLiveRoom.mapper.SzLiveRoomMapper;
import com.ruoyi.module.szLiveRoom.domain.SzLiveRoom;
import com.ruoyi.module.szLiveRoom.service.ISzLiveRoomService;

/**
 * 直播间Service业务层处理
 * 
 * @author yqq
 * @date 2023-10-18
 */
/**
 * 直播间Service业务层处理
 *
 * @author yqq
 * @date 2023-10-18
 */
@Service
public class SzLiveRoomServiceImpl implements ISzLiveRoomService
{
    private static final String PLAY_DOMAIN="play.hfyinyi.cn";
    private static final String APPNAME="live";
    private static final String DOMAIN="stream.hfyinyi.cn";
    public static final String RTMP = "rtmp://";
    public static final String HTTPS = "https://";
    public static final String STR = "/";
    public static final String STR1 = "?";

    public static final String KEY = "95c6c6cd575734422520c6cdfccc4cb6";

    @Autowired
    private SzLiveRoomMapper szLiveRoomMapper;

    /**
     * 查询直播间
     *
     * @param id 直播间主键
     * @return 直播间
     */
    @Override
    public SzLiveRoom selectSzLiveRoomById(Long id)
    {
        return szLiveRoomMapper.selectSzLiveRoomById(id);
    }

    /**
     * 查询直播间列表
     *
     * @param szLiveRoom 直播间
     * @return 直播间
     */
    @Override
    public List<SzLiveRoom> selectSzLiveRoomList(SzLiveRoom szLiveRoom)
    {
        return szLiveRoomMapper.selectSzLiveRoomList(szLiveRoom);
    }

    /**
     * 新增直播间
     *
     * @param szLiveRoom 直播间
     * @return 结果
     */
    @Override
    public int insertSzLiveRoom(SzLiveRoom szLiveRoom)
    {
        //获取播放地址
        String pullUrl=getPullUrl(szLiveRoom.getStreamName());
        szLiveRoom.setPullRtmpUrl(pullUrl);
        szLiveRoom.setStatus("1");
        szLiveRoom.setCreateTime(LocalDateTime.now());
        System.out.println("新增直播间参数:"+szLiveRoom);
        return szLiveRoomMapper.insertSzLiveRoom(szLiveRoom);
    }

    /**
     * 获取播放地址
     * @param streamName
     * @return
     */
    private static String getPullUrl(String streamName) {
        String pullTxSecret = ZBUtil.getSafeUrl(KEY, streamName, ZBUtil.getTime());
        return new StringBuilder().append(HTTPS)
                .append(PLAY_DOMAIN)
                .append(STR)
                .append(APPNAME)
                .append(STR)
                .append(streamName)
                .append(".flv?")
                .append(pullTxSecret).toString();
    }

    /**
     * 获取推流地址
     * @param streamName
     * @return
     */
    private static String getPushUrl(String streamName) {
        //获取推流地址
        String pushTxSecret = ZBUtil.getSafeUrl(KEY, streamName, ZBUtil.getTime());
        String pushUrl = new StringBuilder().append(RTMP)
                .append(DOMAIN)
                .append(STR)
                .append(APPNAME)
                .append(STR)
                .append(streamName)
                .append(STR1)
                .append(pushTxSecret).toString();
        return pushUrl;
    }



    /**
     * 修改直播间
     *
     * @param szLiveRoom 直播间
     * @return 结果
     */
    @Override
    public int updateSzLiveRoom(SzLiveRoom szLiveRoom)
    {
        return szLiveRoomMapper.updateSzLiveRoom(szLiveRoom);
    }

    /**
     * 批量删除直播间
     *
     * @param ids 需要删除的直播间主键
     * @return 结果
     */
    @Override
    public int deleteSzLiveRoomByIds(Long[] ids)
    {
        return szLiveRoomMapper.deleteSzLiveRoomByIds(ids);
    }

    /**
     * 删除直播间信息
     *
     * @param id 直播间主键
     * @return 结果
     */
    @Override
    public int deleteSzLiveRoomById(Long id)
    {
        return szLiveRoomMapper.deleteSzLiveRoomById(id);
    }

    /**
     * 获取直播推流地址
     * @return
     */
    @Override
    public AjaxResult getZbPushUrl() {
        String streamName= UUID.randomUUID().toString().replace("-","");
        // 获取推流地址
        String pushUrl = getPushUrl(streamName);
        Map<String,String> map = new HashMap<>(2);
        map.put("streamName",streamName);
        map.put("pushUrl",pushUrl);
        return AjaxResult.success(map);
    }

    /**
     * 根据streamName修改直播间
     * @param szLiveRoom
     * @return
     */
    @Override
    public AjaxResult editSzLiveRoom(SzLiveRoom szLiveRoom) {
        szLiveRoomMapper.editSzLiveRoom(szLiveRoom);
        return AjaxResult.success();
    }
}
